package com.jzo2o.customer.controller.operation;

import com.jzo2o.api.customer.dto.request.CommonUserPageQueryReqDTO;
import com.jzo2o.api.customer.dto.response.CommonUserResDTO;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.WorkerCertificationAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author DarkClown
 * @date 2024-08-19 22:22
 * @project_name jzo2o-customer
 * @parem
 **/

@RestController("operationWorkerCertificationAuditController")
@RequestMapping("/operation/worker-certification-audit")
@Api(tags = "运营端 - 审核认证接口设计")
public class WorkerCertificationAuditController {

    @Autowired
    WorkerCertificationAuditService workerCertificationAuditService;


    @Autowired
    JwtTool jwtTool;

    @GetMapping("/page")
    @ApiOperation("审核服务人员认证分页查询")
    public PageResult<WorkerCertificationAudit> page(WorkerCertificationAuditPageQueryReqDTO pageQueryReqDTO) {
        return workerCertificationAuditService.selectPage(pageQueryReqDTO);
    }

    @GetMapping("rejectReason")
    @ApiOperation("查询最新的驳回原因")
    public WorkerCertificationAuditResDTO rejectReason(@RequestHeader("Authorization") String token){

        //解析token

        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);

        return   workerCertificationAuditService.rejectReason(currentUserInfo.getId());


    }

    @PutMapping("/audit/{id}")
    @ApiOperation("审核服务人员认证信息")
    public void audit(@RequestBody WorkerCertificationAudit workerCertificationAudit,@PathVariable("id")Long id,@RequestHeader("Authorization") String token){
        CurrentUserInfo currentUserInfo = jwtTool.parseToken(token);
        //根据Id查询
        WorkerCertificationAudit workerCertificationAudit1 = workerCertificationAuditService.getById(id);

        workerCertificationAudit1.setRejectReason(workerCertificationAudit.getRejectReason());
        workerCertificationAudit1.setCertificationStatus(workerCertificationAudit.getCertificationStatus());
        workerCertificationAudit1.setAuditorId(workerCertificationAudit.getAuditorId());
        boolean b = workerCertificationAuditService.saveOrUpdate(workerCertificationAudit1);
        //b为false时抛出异常
        if (!b){
            throw new RuntimeException("审核失败");
        }


    }

}
